﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace shop.Areas.admin.Controllers
{
    [Authorize]
    public class DanhMucController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /admin/DanhMuc/

        public ActionResult Index()
        {
            return View(db.DanhMucs.ToList());
        }

        //
        // GET: /admin/DanhMuc/Details/5

        public ActionResult Details(int id = 0)
        {
            DanhMuc danhmuc = db.DanhMucs.Find(id);
            if (danhmuc == null)
            {
                return HttpNotFound();
            }
            return View(danhmuc);
        }

        //
        // GET: /admin/DanhMuc/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /admin/DanhMuc/Create

        [HttpPost]
       // [ValidateAntiForgeryToken]
        public ActionResult Create(DanhMuc s)
        {

            if(s.Cha==true)
            {
                s.ViTri = "1";
            }
            else
            {
                s.ViTri = null;
            }
            Guid t = Guid.NewGuid();
            s.MaDM = t.ToString();
            Random r = new Random();
            s.IDCha = r.Next(245, 7847567);
            if (ModelState.IsValid)
            {
                 
                db.DanhMucs.Add(s);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(s);
        }

        //
        // GET: /admin/DanhMuc/Edit/5

        public ActionResult Edit(int id = 0)
        {
            DanhMuc danhmuc = db.DanhMucs.Find(id);
            ViewBag.IDCon = danhmuc.IDCon; 
            if (danhmuc == null)
            {
                return HttpNotFound();
            }
            return View(danhmuc);
        }

        //
        // POST: /admin/DanhMuc/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(DanhMuc danhmuc)
        {
            if (ModelState.IsValid)
            {
                db.Entry(danhmuc).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(danhmuc);
        }

        //
        // GET: /admin/DanhMuc/Delete/5

        //public ActionResult Delete(int id = 0)
        //{
        //    DanhMuc danhmuc = db.DanhMucs.Find(id);
        //    if (danhmuc == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(danhmuc);
        //}

        //
        // POST: /admin/DanhMuc/Delete/5

        [HttpPost, ActionName("Delete")]      
        public ActionResult Delete(int id)
        {
            DanhMuc danhmuc = db.DanhMucs.Find(id);
            try
            {
                db.DanhMucs.Remove(danhmuc);
                db.SaveChanges();
                return Content(Boolean.TrueString);
            }
            catch
            {
                return Content(Boolean.FalseString);
            }
           
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}